import { Controller, Post, HttpStatus, HttpCode, Body, Param, Delete, Patch, Get, Query, ParseIntPipe } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiCreatedResponse, ApiOkResponse } from '@nestjs/swagger';
import adminConfig from '@src/config/admin.config';
import { RoleService } from '@src/service/admin/role/role.service';
import { ParsePrimarykey } from '@src/shared/pipe/parse.primaryKey';
import { RoleAccessService } from '@src/service/admin/roleAccess/role-access.service';
import { UpdateRoleDto, AssiginAccessDto, CreateRoleDto, RoleRep } from './dto/index';
import { sqlWhere } from '@src/utils';

@ApiTags('api-role')
@ApiBearerAuth()
@Controller(`${adminConfig.adminPath}/role`)
export class RoleController {
  constructor(
    private roleService: RoleService
  ) { }

  /**
   * @Description: 添加角色
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '创建角色', description: '输入角色名称' })
  @ApiCreatedResponse({
    type: CreateRoleDto,
    description: '创建角色DTO'
  })
  @ApiOkResponse({ type: RoleRep })
  @Post('create')
  @HttpCode(HttpStatus.CREATED)
  async createRole(@Body() createRoleDto: CreateRoleDto): Promise<RoleRep> {
    const role = await this.roleService.create(createRoleDto);
    const { title } = role;
    const result = await this.roleService.checkUnique(title);
    if (result) {
      await this.roleService.save(role);
    }
    return role;
  }

  /**
   * @Description: 删除数据
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '删除角色', description: '输入角色名称' })
  @Get('delete/:id')
  @HttpCode(HttpStatus.OK)
  async deleteById(@Param('id', new ParsePrimarykey()) id: string): Promise<string> {
    const { affected } = await this.roleService.deleteById(id);
    if (affected) {
      return `成功删除id=${id}数据`;
    } else {
      return `删除数据id=${id}失败`;
    }
  }

  /**
   * @Description: 更新角色
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '修改角色', description: '输入角色名称' })
  @ApiCreatedResponse({
    type: UpdateRoleDto,
    description: '修改角色DTO'
  })
  @ApiOkResponse({ type: RoleRep })
  @Patch('modify/:id')
  @HttpCode(HttpStatus.OK)
  async updateRole(@Param('id', new ParsePrimarykey()) id: string, @Body() updateRoleDto: UpdateRoleDto): Promise<RoleRep> {
    return await this.roleService.updateById(id, updateRoleDto);;
  }


  /**
   * @Description: 查询单个角色
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '查询某一个角色', description: '输入角色id查询角色' })
  @ApiOkResponse({ type: RoleRep })
  @Get('getInfo/:id')
  @HttpCode(HttpStatus.OK)
  async findById(@Param('id', new ParsePrimarykey()) id: string): Promise<RoleRep> {
    return await this.roleService.findById(id);
  }

  /**
   * @Description: 查询全部的角色
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '查询全部角色', description: '查询全部的角色' })
  @ApiOkResponse({ type: [RoleRep] })
  @Get('list')
  @HttpCode(HttpStatus.OK)
  async roleList(@Query() queryOption: { [propsName: string]: any }): Promise<RoleRep[]> {
    const {isPage,...other} = queryOption;
    let result;
    if(isPage == 'true'){
      result = await this.roleService.findPage({...other});
    }else{
      result = await this.roleService.findNoPage({...other})
    }
    return result;
  }

  /**
   * @Description: 给角色分配资源
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '给角色分配权限', description: '传递roleId和accessList数组' })
  @Post('assigin/access')
  @HttpCode(HttpStatus.OK)
  async assignAccess(@Body() body: AssiginAccessDto): Promise<any> {
    return await this.roleService.assignAccess(body);
  }
}
